package com.huawei.hwclouds.lubanops.demo.order.client;

import java.util.HashMap;
import java.util.Map;

import com.huawei.hwclouds.lubanops.demo.common.client.HttpCallResult;
import com.huawei.hwclouds.lubanops.demo.common.client.HttpClient;
import com.huawei.hwclouds.lubanops.demo.common.client.LubanopsClientBase;
import com.huawei.hwclouds.lubanops.demo.common.model.HttpHeaderMap;
import com.huawei.hwclouds.lubanops.demo.common.util.ConfigServiceUtil;

/**
 * APM MASTER 相关的服务
 */
public class OrderServiceClient extends LubanopsClientBase {

    private static final String CONFIG_SERVER_ADDRESS = ConfigServiceUtil.getConfigServerAddress(
        "order-service-client.properties", OrderHeaderFactory.class);
    private static final HttpClient CLIENT = ConfigServiceUtil.getHttpClient(
        "order-service-client.properties", OrderHeaderFactory.class);
    private static final String API_ADDRESS_PREFIX = "/apm2/demo/order";

    private OrderServiceClient() {
    }

    /**
     * 此方法用于初始化这个类，读取配置文件，判断合法性 <br>
     *
     * @author y00438022
     * @since 2020年3月24日
     */
    public static void init() {

    }

    /**
     * 获取系统所有的aksk信息，用于注册和心跳 <br>
     *
     * @author l00471413
     * @since 2020年04月03日
     */
    public static void getProductDetail(int productId) {
        String url = CONFIG_SERVER_ADDRESS + API_ADDRESS_PREFIX + "v1/getProductDetail?productId=" + productId;
        CLIENT.sendGet(url, null, HttpHeaderMap.getDefaultHeader());
    }

    public static void addOrders(String orderModels) {
        String url = CONFIG_SERVER_ADDRESS + API_ADDRESS_PREFIX + "/add";
        CLIENT.sendPostJSON(url, HttpHeaderMap.getDefaultHeader(), orderModels);
    }

    public static void deleteOrders(String orderModels) {
        String url = CONFIG_SERVER_ADDRESS + API_ADDRESS_PREFIX + "/delete/" + orderModels;
        CLIENT.sendGet(url, null, HttpHeaderMap.getDefaultHeader());
    }

    public static void clear() {
        String url = CONFIG_SERVER_ADDRESS + API_ADDRESS_PREFIX + "/clear";
        CLIENT.sendGet(url, null, HttpHeaderMap.getDefaultHeader());
    }

    public static void orderInfo(String userId) {
        String url = CONFIG_SERVER_ADDRESS + API_ADDRESS_PREFIX + "/info";
        Map<String, String> param = new HashMap<>();
        param.put("userId", userId);
        CLIENT.sendGet(url, param, HttpHeaderMap.getDefaultHeader());
    }

    public static String productInfo(int id) {
        String url = CONFIG_SERVER_ADDRESS + API_ADDRESS_PREFIX + "/product/" + id;
        HttpCallResult result = CLIENT.sendGet(url, HttpHeaderMap.getDefaultHeader());
        return result.getContent();
    }

}
